﻿// <copyright file="TrendAnalysisUC.cs" company="The University of Scranton"> 
// Copyright (c) 2010 All Rights Reserved, http://sdpmt.codeplex.com 
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY  
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
// PARTICULAR PURPOSE. 
// 
// </copyright> 
// <author>Luke Benedict</author> 
// <email>luke.benedict@ieee.org</email> 
// <date>2010-11-04</date> 
// <summary>Contains a static class that facilitates Trend Analysis</summary>

#region Namespace Usage

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sdpmt.Minitabinterface;
using Sdpmt.Filereader;

#endregion

namespace Sdpmt
{
    /// <summary>
    /// A UserControl that allows the user to do trend analysis
    /// </summary>
    public partial class TrendAnalysisUC : UserControl
    {
        #region Internal Properties

        /// <summary>
        /// Sets the SortedDictionary that represents the software defect data to be analyzed
        /// </summary>
        internal System.Collections.Generic.IDictionary<DateTime, double> SortedDictionary
        {
            set
            {
                m_SortedDictionary = new SortedDictionary<DateTime, double>(value);
                m_PictureBox.Image = null;
            }
        }

        #endregion

        #region Private Members

        /// <summary>
        /// The data to be analyzed
        /// </summary>
        private System.Collections.Generic.IDictionary<DateTime, double> m_SortedDictionary;

        /// <summary>
        /// The length of the forecast period
        /// </summary>
        private int m_ForecastPeriod;

        #endregion

        #region Constructor

        /// <summary>
        /// Standard CTOR
        /// </summary>
        public TrendAnalysisUC()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Handles when the user clicks the button to initiate trend analysis
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_TrendPlotButton_Click(object sender, EventArgs e)
        {
            MinitabBroker mtb;
            IEnumerable<KeyValuePair<DateTime, double>> list;
            IEnumerable<KeyValuePair<DateTime, double>> list2;
            Tuple<Image,IDictionary<DateTime,double>> tup;

            mtb = new MinitabBroker();

            tup = mtb.GetForecast(m_SortedDictionary, m_ForecastPeriod, MinitabConstants.TrendModelType.SCURVE);

            m_PictureBox.Image = tup.Item1;

            list = from stuff in m_SortedDictionary 
                   select stuff;

            m_FittedLinePlotDataGridView.DataSource = list.ToList();
            m_FittedLinePlotDataGridView.Columns[0].HeaderText = "Date";
            m_FittedLinePlotDataGridView.Columns[1].HeaderText = "Cumulative Defects";

            list2 = from stuff in tup.Item2
                    select stuff;

            m_ForecastDataGridView.DataSource = list2.ToList();
            m_ForecastDataGridView.Columns[0].HeaderText = "Date";
            m_ForecastDataGridView.Columns[1].HeaderText = "Cumulative Defects";
        }

        /// <summary>
        /// Handles when the user enters a forecast period into the textbox for forecast period
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            TextBox t = sender as TextBox;

            m_ForecastPeriod = int.Parse("30");

        }

        #endregion
    
    } // end class

} // end namespace
